package com.huaxin.common.view.wheelview;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.RequestManager;
import com.huaxin.common.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by huanghaibin
 * on 16-6-2.
 */
public class BannerShow extends RelativeLayout implements ViewPager.OnPageChangeListener {
    private ViewPager vp_show;
    private List<BannerView> bannerViews = new ArrayList<>();
    private NewsPagerAdapter adapter;
    private CirclePagerIndicator indicator;
    private TextView tv_news_title;
    private ScheduledExecutorService mSchedule;
    private int mCurrentItem = 0;
    private Handler handler;
    private boolean isMoving = false;
    private boolean isScroll = false;
    private RequestManager glidManger;
    private LayoutInflater mInflater;


    public BannerShow(Context context) {
        super(context);
        init(context);
    }

    public BannerShow(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }



    private void init(Context context) {
        mInflater = LayoutInflater.from(context);
        mInflater.inflate(R.layout.banner_show, this, true);
        vp_show = (ViewPager) findViewById(R.id.vp_news);
        indicator = (CirclePagerIndicator) findViewById(R.id.indicator);
        tv_news_title = (TextView) findViewById(R.id.tv_news_title);
        adapter = new NewsPagerAdapter();
        vp_show.setAdapter(adapter);
        indicator.bindViewPager(vp_show);
        vp_show.addOnPageChangeListener(this);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                vp_show.setCurrentItem(mCurrentItem);
            }
        };
        mSchedule = Executors.newSingleThreadScheduledExecutor();
        mSchedule.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                if (!isMoving && !isScroll) {
                    mCurrentItem = (mCurrentItem + 1) % bannerViews.size();
                    handler.obtainMessage().sendToTarget();
                }
            }
        }, 2, 4, TimeUnit.SECONDS);

    }
    public void initData(Context context) {
        this.bannerViews.clear();
        Banner[] banners =Banner.values();
        for (Banner banner : banners) {
            BannerView bannerView = new BannerView(context,banner);
            this.bannerViews.add(bannerView);
        }
        adapter.notifyDataSetChanged();
        indicator.notifyDataSetChanged();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        isMoving = mCurrentItem != position;
    }

    @Override
    public void onPageSelected(int position) {
        isMoving = false;
        mCurrentItem = position;
        isScroll = false;
        tv_news_title.setText(bannerViews.get(position).getTitle());
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        isMoving = state != ViewPager.SCROLL_STATE_IDLE;
        isScroll = state != ViewPager.SCROLL_STATE_IDLE;
    }




    private class NewsPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return  bannerViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(bannerViews.get(position).getImg());

        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(bannerViews.get(position).getImg());
            return bannerViews.get(position).getImg();

        }
    }

    public class BannerView{
        private  Context context;
        private ImageView img;

        public ImageView getImg() {
            return img;
        }

        public String getTitle() {
            return banner.getTitle();
        }

        private Banner banner;

        BannerView(Context context,Banner banner){
            this.context =context;
            this.banner = banner;
            init();
        }

        private void init() {
            img = new ImageView(context);
            img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            img.setImageResource(banner.getRes());
        }


    }
}
